// 浏览器缓存的 key
const storageKey = '__eight_data_key__'

// 初始数据 TODO: 要增减字段改这里
const baseDataStruct = {
    reviewList: [
        {
            title: '第八周交易计划2 《我的信仰》',
            date: '2017-01-20',
            cover: './images/past-pic1.png'
        },
        {
            title: '第八集：巅峰之战！谁是黑马？',
            date: '2017-01-20',
            cover: './images/past-pic2.png'
        },
        {
            title: '巨债承压动真格，赚钱才是真本事！',
            date: '2017-01-20',
            cover: './images/past-pic3.png'
        },
    ],
    issueList: [
        {
            name: '扭转乾坤',
            city: '温州',
            time: '10:06',
            like: 0,
            desc: '大部分赌徒都是赌久必输，而且只有部分职业赌徒是赌久必赢的。所以说，赌久必输是生活中常见的现象'
        },
        {
            name: '扭转乾坤',
            city: '广州',
            time: '09:34',
            like: 0,
            desc: '交易员是猎人，捕捉市场动态；交易员是心理分析师，分析别人交易心态的同时也被别人分析着'
        }
    ],
}

let dataRecord = null
let isLogin = false


// 保存数据
const saveData = () => {
    localStorage.setItem(storageKey, JSON.stringify(dataRecord))
    console.log('数据保存成功')
}

// 数据初始化
const loginPage = () => {
    let d = localStorage.getItem(storageKey)
    return new Promise((resolve, reject) => {
        if (d !== null) {
            console.log('成功读取存档')
            dataRecord = JSON.parse(d)
            isLogin = true
            resolve()
        } else {
            console.log('木有存档')
            dataRecord = JSON.parse(JSON.stringify(baseDataStruct))
            isLogin = true
            resolve()
        }
    })

}

const getReviewList = () => {
    if (!isLogin) {
        console.log('请先登录')
        return
    }

    return Promise.resolve(dataRecord.reviewList)
}

const getIssueList = () => {
    if (!isLogin) {
        console.log('请先登录')
        return
    }

    return Promise.resolve(dataRecord.issueList)
}

const addIssue = (issue) => {
    if (!isLogin) {
        console.log('请先登录')
        return
    }
    dataRecord.issueList.push(issue)
    saveData()
    return Promise.resolve()
}

export {
    loginPage,
    getReviewList,
    getIssueList,
    addIssue
}